﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;

using MW.Service;
using MW.Domain.DC;

namespace MW.Web
{
    public partial class Detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MetaDC mdcTemp = MetaService.GetInstance().GetMetaDCByUniqueID(URLUniqueID);
                this.ltlContributor.Text = mdcTemp.Contributor.Replace("|", "&nbsp;");
                this.ltlCoverage.Text = mdcTemp.Coverage.Replace("|", "&nbsp;");
                this.ltlCreator.Text = mdcTemp.Creator.Replace("|", "&nbsp;");
                this.ltlDate.Text = mdcTemp.Date.Replace("|", "&nbsp;");
                this.ltlDescription.Text = mdcTemp.Description.Replace("|", "<br />");
                this.ltlFormat.Text = mdcTemp.Format.Replace("|", "&nbsp;");
                this.ltlIdentifier.Text = mdcTemp.Identifier.Replace("|", "&nbsp;");
                this.ltlLanguage.Text = mdcTemp.Language.Replace("|", "&nbsp;");
                this.ltlPublisher.Text = mdcTemp.Publisher.Replace("|", "&nbsp;");
                this.ltlRelation.Text = mdcTemp.Relation.Replace("|", "&nbsp;");
                this.ltlRights.Text = mdcTemp.Rights.Replace("|", "&nbsp;");
                this.ltlSource.Text = mdcTemp.Source.Replace("|", "&nbsp;");
                this.ltlSubject.Text = mdcTemp.Subject.Replace("|", "&nbsp;");
                this.ltlTitle.Text = mdcTemp.Title.Replace("|", "&nbsp;");
                this.ltlType.Text = mdcTemp.Type.Replace("|", "&nbsp;");

                this.ltlTitleHeader.Text = this.ltlTitle.Text;
                this.ltlMARCXML.Text = BuildMARCInfo(mdcTemp.MARC_XML);
                
                this.lvResult.DataSource = MetaService.GetInstance().GetRelatedMeta(mdcTemp);
                this.lvResult.DataBind();
            }
        }

        #region Property

        private string URLUniqueID
        {
            get
            {
                if (string.IsNullOrEmpty(Request["uid"]))
                {
                    Response.Redirect("Default.aspx");
                }
                return Request["uid"];
            }
        }

        private string BuildMARCInfo(string strMARC_XML) 
        {
            string strReturn = "<table>";

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(strMARC_XML);
            for (int i = 0; i < doc.ChildNodes[0].ChildNodes.Count; i++)
            {
                strReturn += AddMARCNode(doc.ChildNodes[0].ChildNodes[i]);
            }

            return strReturn += "</table>";
        }

        private string AddMARCNode(XmlNode xnField)
        {
            string strReturn = "";

            if (xnField.Name == "leader")
            {
                strReturn += "<tr><td>LDR</td><td>"
                    + xnField.FirstChild.Value + "</td></tr>";
            }
            else if (xnField.Name == "controlfield")
            {
                strReturn += "<tr><td>" + xnField.Attributes["tag"].Value + "</td><td>"
                    + xnField.FirstChild.Value + "</td></tr>";
            }
            else if (xnField.Name == "datafield")
            {
                strReturn += "<tr><td>" + xnField.Attributes["tag"].Value + "</td><td>";

                for (int i = 0; i < xnField.ChildNodes.Count; i++)
                {
                    if (xnField.ChildNodes[i].Attributes != null
                        && xnField.ChildNodes[i].Attributes["code"] != null)
                    {
                        strReturn += "|" + xnField.ChildNodes[i].Attributes["code"].Value + "&nbsp;"
                            + xnField.ChildNodes[i].FirstChild.Value + "&nbsp;";
                    }
                }
                strReturn += "</td></tr>";
            }

            return strReturn;
        }

        #endregion

        #region Event

        protected void btnSearchHeader_Click(object sender, EventArgs e)
        {
            if (this.txtSearchHeader.Text.Trim().Length > 0)
            {
                Response.Redirect("Result.aspx?q=" + Server.HtmlEncode(this.txtSearchHeader.Text.Trim()));
            }
        }

        #endregion
    }
}
